package com.examination.client.util;

import com.examination.client.enums.ResultEnum;

/**
 * @author: XiaoDong
 * @date: 2021-10-16 20:51
 * @description:
 */
public class Result<T> {
    private Integer code;
    private String msg;
    private T data;

    public Result() {
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public Result setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Result setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result setData(T data) {
        this.data = data;
        return this;
    }

    public Result setCode(ResultEnum resultEnum) {
        this.code = resultEnum.code;
        return this;
    }


    public static <T> Result<T> success() {
        Result result = new Result();
        result.setCode(ResultEnum.SUCCESS);
        result.setMsg(ResultEnum.SUCCESS.msg);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result result = new Result();
        result.setCode(ResultEnum.SUCCESS.code);
        result.setMsg(ResultEnum.SUCCESS.msg);
        result.setData(data);

        return result;
    }


    public static <T> Result<T> fail() {
        Result result = new Result();
        result.setCode(ResultEnum.FAIL);
        result.setMsg(ResultEnum.FAIL.msg);
        return result;
    }


    public static <T> Result<T> fail(String msg) {
        Result result = new Result();
        result.setCode(ResultEnum.FAIL).setMsg(msg);
        return result;
    }

    public static <T> Result<T> fail(int code, String msg){
        Result result = new Result();
        result.setCode(code).setMsg(msg);
        return result;
    }
}
